const MongoClient = require('mongodb').MongoClient;
const url = "mongodb://taosha_user:apyl8XWP6LSW4JHs@101.35.6.34:27722/taosha";

async function main() {
  const client = await MongoClient.connect(url, {useNewUrlParser: true, useUnifiedTopology: true});
  const db = client.db('taosha');


  let topics = await db.collection('topics')
    .find(
      {content: /passport.weibo.com/}
    )
    .toArray();


  for (let i = 0; i < topics.length; i++) {
    let topic = topics[i];
    let original_url = topic.original_url;

    original_url = original_url
      .replace(/.+(https:\/\/s.weibo.com.+&Refer=top).+/, '$1')
      .replace(/.+(https:\/\/s.weibo.com.+)&domain.+/, '$1')
      .replace(/.+(https:\/\/weibo.com.+)\?from=.+/, '$1')
      .replace('/tv/tv/', '/tv/');
    if (original_url.startsWith('https://passport.weibo.com')) {
      original_url = `https://s.weibo.com/weibo?q=${topic.title}`;
    }

    try {
      let result = await db.collection('topics').updateOne({_id: topic._id}, {
        $set: {
          original_url: original_url,
          content: original_url,
        },
      });
      console.log(result.result);
    } catch (e) {
      console.error(e.message, topic.real_author_name);
      await db.collection('topics').deleteOne({_id: topic._id});
    }
  }

  client.close();

}

main();
